package com.study.thread.demo;

import com.study.thread.demo.future.FutureDemo;

import java.util.concurrent.*;

public class TestFutureDemo {

    public static void main(String[] args) throws Exception {
        String queryStr1 = "query1";
        String queryStr2 = "query2";
        FutureTask<String> future1 = new FutureTask<>(new FutureDemo(queryStr1));
        FutureTask<String> future2 = new FutureTask<>(() -> queryStr2 + " 处理完成！");

        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.submit(future1);
        executorService.submit(future2);
        Future<String> future3 = executorService.submit(new FutureDemo("123"));
        Future future4 = executorService.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("123");
            }
        }, "123");

        System.out.println("执行中...");
        //处理其他相关的任务。
        TimeUnit.SECONDS.sleep(2);
        String result1 = future1.get();
        String result2 = future2.get();
        String result3 = future3.get();
        String result4 = (String) future4.get();

        System.out.println("数据处理完成。。" + result1);
        System.out.println("数据处理完成。。" + result2);
        System.out.println("数据处理完成。。" + result3);
        System.out.println("数据处理完成。。" + result4);

        executorService.shutdownNow();

        new Thread(future1).start();
        System.out.println(future1.get());
    }
}
